<?php
namespace V1;
class UserPushId extends \ResourceClass {
    public $isActive = true;
    public $remark = '用户上传推送消息ID';

    public function post(array $req, array $post, &$result) {
        if (!isset($post['uid'])) {
            return 'require user id';
        }
        if (!isset($post['regid'])) {
            return 'require registration id';
        }
        if (!isset($post['platform'])) {
            return 'require platform';
        }
        if (!isset($post['device'])) {
            return 'require device info';
        }

        $platform = $post['platform'];
        $user_id = $post['uid'];
        $reg_id = $post['regid'];
        $device = $post['device'];

        // 查纪录
        $log_cls = \Biz\LogHandler::getAppMessage($platform, $reg_id, $user_id);
        if (!is_object($log_cls) && $log_cls !== false) {
            return 'failed';
        }
        // 若不存在则创建
        if (!is_object($log_cls) && $log_cls === false) {
            $new_log_cls = new \Cls\Log\AppMessage();
            $new_log_cls->create($platform, $reg_id, $user_id, $device);
            if (\Biz\LogHandler::appMessageRegister($new_log_cls) !== true) {
                return 'failed';
            }
        }else{
            // 若存在则+1
            $count = $log_cls->getCount() + 1;
            $log_cls->setCount($count);
            if (\Biz\LogHandler::appMessageUpdate($log_cls) !== true) {
                return 'failed';
            }
        }
        return '';
    }
}